<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        
        .wrap {
            width: 200px;
            height: 200px;
        }
        
        ul li {
            list-style: none;
            height: 200px;
            width: 200px;
        }
        
        ul li:nth-child(1) {
            background-color: blue;
        }
        
        ul li:nth-child(2) {
            background-color: chocolate;
        }
        
        ul li:nth-child(3) {
            background-color: red;
        }
        
        ul li:nth-child(4) {
            background-color: black;
        }
        
        .wrap {
            position: relative;
        }
        
        ul li {
            position: absolute;
        }
        
        img {
            height: 100%;
            width: 100%;
        }
        
        .btn {
            width: 50px;
            height: 100px;
            position: absolute;
            top: 20px;
            z-index: 1000;
        }
        
        .item.active {
            z-index: 10;
        }
        
        #goPr {
            left: 0px;
        }
        
        #goPre {
            right: 0px;
        }
    </style>
</head>

<div class="wrap">
    <ul class="list">
        <li class="item active">
            <img src="img/portrait-1.jpg" alt="">
        </li>
        <li class="item">
            <img src="img/portrait-2.jpg" alt="">
        </li>
        <li class="item">
            <img src="img/portrait-3.jpg" alt="">
        </li>
        <li class="item">
            <img src="img/portrait-4.jpg" alt="">
        </li>
    </ul>
    <input type="button" class="btn" id="goPr" value="<">
    <input type="button" class="btn" id="goPre" value=">">
</div>
<script>
    let items = document.getElementsByClassName('item');
    let goPre = document.getElementById('goPr');
    let goNext = document.getElementById('goPre');
    let index = 0;
    let goindex = function() {
        for (let i = 0; i < items.length; i++) {
            items[i].className = 'item';
        }
        items[index].className = 'item active';
    }
    let goNextpr = function() {
        if (index < 3) {
            index++;
        } else {
            index = 0;
        }

        goindex();
    }
    let goPrepr = function() {
        if (index == 0) {
            index = 3;
        } else {
            index--;
        }

        goindex();
    }
    goNext.addEventListener('click', function() {
        goNextpr();
    })
    goPre.addEventListener('click', function() {
        goPrepr();
    })
</script>
</body>

</html>